package com.iseastar.guojiang.team.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.CourierTeamMonthBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.team.CourierTeamCenterDetailActivity;
import com.iseastar.guojiang.team.adapter.OperationTeamZoneAdapter;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.SListView;

/**
 * @author 李澄锋<br>
 * 2018/6/13
 */
public class OperationTeamMonthFragment extends BaseFragment {

    private int type;// 0 月总收入  1 平均评分  2 人均月里程   3  人均月单量
    private SListView mListView = null;
    private OperationTeamZoneAdapter mAdapter = null;
    private boolean isRequest = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt("type");
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_operation_team_month);
        super.findViewById();

        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new OperationTeamZoneAdapter(null, getActivityContext());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CourierTeamMonthBean item = (CourierTeamMonthBean) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getActivityContext(), CourierTeamCenterDetailActivity.class);
                intent.putExtra("teamId", item.getTeamId());
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getZoneTeamList(type + 1, 0);
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_team_zone_list:
                cancelLoadingDialog();
                if (msg.arg1 == type + 1) {
                    onRefreshComplete();
                    final ReqResult<CourierTeamMonthBean> reqResult = JSON.parseList(msg.obj, CourierTeamMonthBean.class);
                    if (checkLoginStatus(reqResult)) {
                        getActivityContext().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mAdapter == null) {
                                    mAdapter = new OperationTeamZoneAdapter(reqResult.getResultList(), getActivityContext());
                                    mListView.setAdapter(mAdapter);
                                } else {
                                    mAdapter.setItems(reqResult.getResultList(), true);
                                }
                            }
                        });
                    } else {
                        showToast(reqResult.getMessage());
                    }
                }
                return false;
            //申请加入战队成功
            case MsgID.courier_team_apply_team:
                cancelLoadingDialog();
                ReqResult<?> reqResult2 = JSON.parser(msg.obj);
                if (reqResult2.isSuccess()) {
                    AppHttp.getInstance().getZoneTeamList(type + 1, 0);
                } else {
                    showToast(reqResult2.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (isRequest){
            showLoadingDialog(null);
            AppHttp.getInstance().getZoneTeamList(type + 1, 0);
        }
        isRequest = false;
    }

    @Override
    public void onStop() {
        super.onStop();
        isRequest = true;
    }

    public void refreshData(){
        showLoadingDialog(null);
        AppHttp.getInstance().getZoneTeamList(type + 1, 0);
    }
}
